/*
 * 作者：姚俊
 * 功能：专利的超级检索的action
 * 日期：2015-06-26
 */
package admin.action.superSearch;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import admin.action.fruits.Constant;
import admin.service.fruits.PatentService;

import teacher.bean.Patent;
import tools.StringDate;

public class PatentSuperAction {
	
	private List<Patent> patents;
	private Patent patent;
	private String applyDate2;
	private String empowerDate2;
	private int direction;
	//当前页
	private int currentPage;
	//最后一页
	private int endPage;
	private int counts;
	private int flag;
	private int code;
	private String exp;
	private String emp;

	
	
	
	
	public String getExp() {
		return exp;
	}
	public void setExp(String exp) {
		this.exp = exp;
	}
	public String getEmp() {
		return emp;
	}
	public void setEmp(String emp) {
		this.emp = emp;
	}
	public List<Patent> getPatents() {
		return patents;
	}
	public void setPatents(List<Patent> patents) {
		this.patents = patents;
	}
	public Patent getPatent() {
		return patent;
	}
	public void setPatent(Patent patent) {
		this.patent = patent;
	}
	public String getApplyDate2() {
		return applyDate2;
	}
	public void setApplyDate2(String applyDate2) {
		this.applyDate2 = applyDate2;
	}
	public String getEmpowerDate2() {
		return empowerDate2;
	}
	public void setEmpowerDate2(String empowerDate2) {
		this.empowerDate2 = empowerDate2;
	}
	
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public int getCounts() {
		return counts;
	}
	public void setCounts(int counts) {
		this.counts = counts;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	
	
	
	public String execute()
	{
		this.setFlag(4);
//	System.out.println("种类我i:"+this.getExp());
		if(this.getPatent()!=null)
		{
			if(this.getPatent().getName()==null||this.getPatent().getName().equals(""))
				this.getPatent().setName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPatent().setName(new String(this.getPatent().getName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPatent().getFiestInventor()==null||this.getPatent().getFiestInventor().equals(""))
				this.getPatent().setFiestInventor(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPatent().setFiestInventor(new String(this.getPatent().getFiestInventor().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPatent().getApplyNumber()==null||this.getPatent().getApplyNumber().equals(""))
				this.getPatent().setApplyNumber(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPatent().setApplyNumber(new String(this.getPatent().getApplyNumber().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPatent().getEmpowerNum()==null||this.getPatent().getEmpowerNum().equals(""))
				this.getPatent().setEmpowerNum(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPatent().setEmpowerNum(new String(this.getPatent().getEmpowerNum().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPatent().getEmpowerUnit()==null||this.getPatent().getEmpowerUnit().equals(""))
				this.getPatent().setEmpowerUnit(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPatent().setEmpowerUnit(new String(this.getPatent().getEmpowerUnit().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPatent().getEmpowerDate()!=null)
				this.getPatent().setEmpowerDate(
						StringDate.StringToDate(
								StringDate.DateToString(
										this.getPatent().getEmpowerDate())));
			if(this.getPatent().getApplyDate()!=null)
				this.getPatent().setApplyDate(
						StringDate.StringToDate(
								StringDate.DateToString(
										this.getPatent().getApplyDate())));
		}
		/**
		 * authour:laohe;
		 */
		List<String> orders=new ArrayList<String>();
		if(this.getExp()!=null&&!(this.getExp().equals("")))
		{
			orders.add(this.getExp());
		}
		if(this.getEmp()!=null&&!(this.getEmp().equals("")))
		{
			orders.add(this.getEmp());
		}
		
		System.out.println("action尺寸为："+orders.size());
		//当前页为零的时候，把当前页变成1
		if(this.getCurrentPage()==0)
			this.setCurrentPage(1);
		
		int add=0;
		//最后一页
		if(new PatentService().getPatentCountByDirection(
				this.getPatent(), this.getEmpowerDate2(),
				this.getApplyDate2(),this.getDirection(),Constant.state)%Constant.pageSize!=0)
		{
			add=1;
		}

		this.setEndPage(
				new PatentService().getPatentCountByDirection(
						this.getPatent(), this.getEmpowerDate2(),
						this.getApplyDate2(),this.getDirection(),Constant.state)/Constant.pageSize+add);
		this.setCounts(new PatentService().getPatentCountByDirection(
				this.getPatent(), this.getEmpowerDate2(),this.getApplyDate2(), this.getDirection(),Constant.state));

        this.setPatents(
		new PatentService().getSegregatePatentByDirection(
				this.getPatent(), this.getEmpowerDate2(),this.getApplyDate2(), this.getDirection(),(this.getCurrentPage()-1)*Constant.pageSize+1,Constant.pageSize,Constant.state,orders));
        
		return "succ";
	}

}
